package org.itboys.product.service;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseService;
import org.itboys.product.entity.ReceiveAddress;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by huml on 2016/4/19.
 * 收货地址业务
 */
@Service
public class ReceiveAddressService extends BaseService<ReceiveAddress, Long> {
    private static final long serialVersionUID = -1164006944716470835L;

    @Resource(name="productDS")
    private MongoDataSource addressDS;

    @Override
    protected MongoDataSource getMongoDataSource() {
        return addressDS;
    }

    @Override
    protected Class<ReceiveAddress> getEntityClass() {
        return ReceiveAddress.class;
    }

    /**
     * 新建地址/编辑地址
     * @param provinceId  省id
     * @param cityId  市id
     * @param districtId  区县id
     * @param address  详细地址
     * @param name  收货人姓名
     * @param mobile  手机号码
     * @param memberId  会员id
     */
    public void save(Long addressId, Long provinceId, Long cityId, Long districtId, String address,
                     String name, String mobile, Long memberId){
        ReceiveAddress receiveAddress ;
        if(addressId == null){
            receiveAddress = new ReceiveAddress();
            receiveAddress.setProvinceId(provinceId);
            receiveAddress.setCityId(cityId);
            receiveAddress.setDistrictId(districtId);
            receiveAddress.setAddress(address);
            receiveAddress.setMemberId(memberId);
            receiveAddress.setName(name);
            receiveAddress.setMobile(mobile);
            receiveAddress.setCreateTime(new Date());
            receiveAddress.setUpdateTime(new Date());
            //如果之前没有地址的话,要把这个地址设为默认地址
            List<ReceiveAddress> addresses = getAddressesByMemberId(memberId);
            if(addresses == null || addresses.isEmpty()){
                receiveAddress.setIsDeleted(ReceiveAddress.DEFAULT_ADDRESS);//默认地址
            }
            super.save(receiveAddress);
        } else {
            receiveAddress = getById(addressId);
            if(provinceId != null){
                receiveAddress.setProvinceId(provinceId);
            }
            if(cityId != null){
                receiveAddress.setCityId(cityId);
            }
            if(districtId != null){
                receiveAddress.setDistrictId(districtId);
            }
            if(StringUtils.isNotBlank(address)){
                receiveAddress.setAddress(address);
            }
            if(StringUtils.isNotBlank(name)){
                receiveAddress.setName(name);
            }
            if(StringUtils.isNotBlank(mobile)){
                receiveAddress.setMobile(mobile);
            }
            super.update(receiveAddress);
        }
    }

    /**
     * 获取会员的地址
     * @param memberId
     * @return
     */
    public List<ReceiveAddress> getAddressesByMemberId(Long memberId){
        Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
        param.put("memberId", memberId);
        param.put("isDeleted !=", 1);
        return super.list(param);
    }

    /**
     * 获取会员的默认地址
     * @param memberId
     * @return
     */
    public ReceiveAddress getMemberDefualtAddress(Long memberId){
        Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
        param.put("memberId", memberId);
        param.put("isDeleted", 2);
        List<ReceiveAddress> list = super.list(param);
        return list != null && !list.isEmpty() ? list.get(0) : null;
    }

    /**
     * 设置默认地址
     * @param memberId  //会员id
     * @param addressId  地址id
     */
    public void setDefaultAddress(Long memberId, Long addressId){
        //先找到原来的默认地址
        ReceiveAddress defaultAddress = getMemberDefualtAddress(memberId);
        ReceiveAddress address = getById(addressId);
        address.setIsDeleted(ReceiveAddress.DEFAULT_ADDRESS);
        if(defaultAddress != null){//如果原来有默认地址,就把原来的默认地址取消掉
            defaultAddress.setIsDeleted(0);
            List<ReceiveAddress> list = Lists.newArrayListWithExpectedSize(2);
            list.add(defaultAddress);
            list.add(address);
            super.batchUpdate(list);
        } else {//没有的话直接设为默认就好了
            super.update(address);
        }
    }

    /**
     * 删除地址
     * @param addressId
     */
    public void deleteAddress(Long addressId){
        ReceiveAddress address = getById(addressId);
        //判断一下删除的地址是不是默认地址,如果是的话,要重新设置默认地址
        if(address.getIsDeleted().equals(ReceiveAddress.DEFAULT_ADDRESS)){
            Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
            param.put("memberId", address.getMemberId());
            param.put("isDeleted", 2);
            List<ReceiveAddress> list = super.list(param);
            if(list == null || list.isEmpty()){//已经没有其他地址, 直接删除现有地址
                super.update(addressId, "isDeleted", 1);
            } else {//有其他地址  选取第一个作为新的默认地址
                ReceiveAddress defaultAddress = list.get(0);
                defaultAddress.setIsDeleted(ReceiveAddress.DEFAULT_ADDRESS);
                address.setIsDeleted(1);
                list = Lists.newArrayListWithExpectedSize(2);
                list.add(defaultAddress);
                list.add(address);
                super.batchUpdate(list);
            }
        } else {//不是默认地址  直接删除
            super.update(addressId, "isDeleted", 1);
        }
    }

}